package org.apache.ibatis.binding;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.ibatis.binding.MapperProxy.MapperMethodInvoker;
import org.apache.ibatis.session.SqlSession;

/**
 * @author 潜行的青衣
 */
public class MybatisCommonsMapperProxyFactory<T> extends MapperProxyFactory<T> {

	/**
	 * @param mapperInterface
	 */
	public MybatisCommonsMapperProxyFactory(Class<T> mapperInterface) {
		super(mapperInterface);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T newInstance(SqlSession sqlSession) {
		Class<T> mapperInterface = null;
		Map<Method, MapperMethodInvoker> methodCache = null;
		try {
			mapperInterface = (Class<T>) FieldUtils.readField(this, "mapperInterface", true);
			methodCache = (Map<Method, MapperMethodInvoker>) FieldUtils.readField(this, "methodCache", true);
			final MapperProxy<T> mapperProxy = new MybatisCommonsMapperProxy<T>(sqlSession, mapperInterface, methodCache);
		    return newInstance(mapperProxy);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

}
